package com.senseuni.da.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.os.Bundle;

import com.elvishew.xlog.XLog;

/**
 * ================================================
 * Copyright (c) 2020 All rights reserved
 * 描述：
 * Author: Yuri
 * Date： Date: 2024/12/8
 * ================================================
 */
public class InstallerApkBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) {
            XLog.d(">>>>>>>ACTION_MY_PACKAGE_REPLACED>>>>>>>");
//            Intent newIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
//            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(newIntent);
            int status = extras.getInt(PackageInstaller.EXTRA_STATUS);
            String message = extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);
            switch (status) {
                case PackageInstaller.STATUS_PENDING_USER_ACTION:
                    break;

                case PackageInstaller.STATUS_SUCCESS:
                    //安装成功
                    XLog.d( "Install succeeded!");
                    break;

                case PackageInstaller.STATUS_FAILURE:
                case PackageInstaller.STATUS_FAILURE_ABORTED:
                case PackageInstaller.STATUS_FAILURE_BLOCKED:
                case PackageInstaller.STATUS_FAILURE_CONFLICT:
                case PackageInstaller.STATUS_FAILURE_INCOMPATIBLE:
                case PackageInstaller.STATUS_FAILURE_INVALID:
                case PackageInstaller.STATUS_FAILURE_STORAGE:
                    XLog.d(  "Install failed!" + status + ", " + message);
                    break;
                default:
                    XLog.d(  "Unrecognized status received from installer: " + status);
            }
        }
    }
}
